<?php

namespace app\store\controller\scenic;

use app\store\model\store\User;
use app\store\controller\Controller;
use app\store\model\store\Shop as ShopModel;
use app\store\model\store\shop\Order as OrderModel;
use app\store\model\Category as CategoryModel;
use app\store\model\Goods as GoodsModel;
use app\store\model\store\Scenic;
use app\store\model\Ticket as TicketModel;
use app\store\service\Goods as GoodsService;
/**
 * 门票记录
 * Class Order
 * @package app\store\controller\shop
 */
class Ticket extends Controller
{
    /**
     * 门票记录列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // // 核销记录列表
        // $model = new OrderModel;
        // $list = $model->getList($shop_id, $search);
        // // 门店列表
        // $shopList = (new ShopModel)->getList();
        // return $this->fetch('index', compact('list', 'shopList'));
        // 获取全部商品列表
        $model = new TicketModel();
        
        $list = $model->getList();
        
        return $this->fetch('index', compact('list'));
    }
    /**
     * 添加门票
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            $model = new Scenic();
            $list = $model->getList();
            //商户信息
            $user = User::where('is_delete', '=', '0')
                    ->where('is_super','<>',1)
                    ->select();
            return $this->fetch('add', compact('list','user'));
        }
        $model = new TicketModel();
        $data  = $this->postData('ticket');
        
        if(isset($data['ticket_set'])){
            $data['ticket_set'] = implode(',',$data['ticket_set']);
        }

        if ($model->add($data)) {
            return $this->renderSuccess('添加成功', url('scenic.ticket/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    // /**
    //  * 一键复制
    //  * @param $id
    //  * @return array|mixed
    //  * @throws \think\exception\PDOException
    //  */
    // public function copy($id)
    // {
    //     // 商品详情
    //     $model = TicketModel::detail($id);
        
    //     if (!$this->request->isAjax()) {
    //         $scenic = new Scenic();
    //         $list = $scenic->getList();
    //         return $this->fetch('edit',compact('model','list'));
    //     }
    //     $model = new TicketModel();
    //     if ($model->add($this->postData('ticket'))) {
    //         return $this->renderSuccess('添加成功', url('scenic.ticket/index'));
    //     }
    //     return $this->renderError($model->getError() ?: '添加失败');
    // }
    

    /**
     * 门票编辑
     * @param $id
     * @return array|bool|mixed
     */
    public function edit($id)
    {
        
        // 门票详情
        $model = TicketModel::detail($id);
       
        if (!$this->request->isAjax()) {
           
            //景区
            $scenic = new Scenic();
            $list = $scenic->getList();
            //商户
            $user = User::where('is_delete', '=', '0')
                    ->where('is_super','<>',1)
                    ->select();
            //门票
            if($model['type'] == 2){ 
                $allTicket = TicketModel::where('scenic_id','=',$model['scenic_id'])
                ->where('type','=',1)
                ->where('is_delete','=',0)
                ->field('id,name')
                ->select();
                
                $arr = explode(',',$model['ticket_set']);
                
                return $this->fetch('edit',compact('model','list','allTicket','arr','user'));
            }
            
            return $this->fetch('edit',compact('model','list','user'));
        }
        //$ticket = new Ticket();
        //$data = $this->postData('ticket');
        
        // $ticket->name =      $data['name'];
        // $ticket->type =      $data['type'];
        // $ticket->scenic_id = $data['scenic_id'];
        // $ticket->price =     $data['price'];
        // $ticket->stock_num = $data['stock_num'];
        // $ticket->status =    $data['status'];
        // $ticket->sort =      $data['sort'];
        // $res = $ticket->save();

        // // 更新记录
        // if ($res) {
        //     return $this->renderSuccess('更新成功', url('shop.order/index'));
        // }
        // return $this->renderError('更新失败');
        // 更新记录
        $ticket = new TicketModel();
        $data = $this->postData('ticket');
        if(isset($data['ticket_set'])){
            $data['ticket_set'] = implode(',',$data['ticket_set']);
        }
        if(!isset($data['ticket_set']) && $data['type'] == 2){
            return $this->renderError('套票集不能为空！');
        }
        if ($ticket->edit($data,$id)) {
            return $this->renderSuccess('更新成功', url('scenic.ticket/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改门票状态
     * @param $id
     * @param boolean $state
     * @return array
     */
    public function state($id, $state)
    {
        // 门票详情
        // $model = Ticket::detail($id);
        // if (!$model->setStatus($state)) {
        //     return $this->renderError('操作失败');
        // }
        // return $this->renderSuccess('操作成功');
        $model = TicketModel::get($id);       
        $model->status = $state == 10 ? 20 : 10;
        if (!$model->save()) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除门票
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        // 门票详情
        // $model = Ticket::detail($id);
        
        // if (!$model->setDelete()) {
        //     return $this->renderError($model->getError() ?: '删除失败');
        // }
        // return $this->renderSuccess('删除成功');
        
        $model = TicketModel::get($id);
        if (!$model->setDelete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     *  获取单票集
     */
    public function getTicketSet($scenic_id)
    {   
        
        $list = TicketModel::where('scenic_id','=',$scenic_id)
                ->where('type','=',1)
                ->where('is_delete','=',0)
                ->field('id,name')
                ->select();
        // $data = $list->toJson();
        // echo '<pre>';
        // print_r($list) ;
        return json(['code'=>1,'data'=>$list]);
    }

}